const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    addressList: [],
    keyword: ''
  },
  keywordInput: function (e) {
    var that = this;
    that.setData({
      keyword: e.detail.value
    });
  },
  onKeywordConfirm: function () {
    
    app.globalData.checkedAddress = {}
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      addressList: []
    })
    this.getAddressList();
  },
  czM(){
    this.setData({
      keyword:''
    })
    app.globalData.checkedAddress = {}
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      addressList: []
    })
    this.getAddressList();

  },
  onShow: function () {
    app.globalData.checkedAddress = null
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      addressList: []
    })
    this.getAddressList();
  },
  getAddressList() {
    isLoad = 1;
    if (hadLastPage) {
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      searchWord: that.data.keyword || ''
    }
    this.setData({
      showLoding: true,
      showNoData: false
    });
    setTimeout(function () {
      common.request(api.pointlist, data, 'POST').then(function (data) {
        that.setData({
          showLoding: false
        });
        if (data.type == 'success') {
          var addressList = that.data.addressList;
          
          for (var i = 0; i < data.data.dataList.length; i++) {
            addressList.push(data.data.dataList[i]);
          }

          if (data.data.pageNo == data.data.totalPages) {
            hadLastPage = true;
          } else {
            pageNo++;
          }
          that.setData({
            addressList: addressList
          });
          isLoad = 0;
          if (data.data.total == 0) {
            that.setData({
              showNoData: true
            });
          }
        } else {
          console.log(data.content);
          isLoad = 0;
        }
      });
    }, 1000);
    // let that = this;
    // common.request(api.pointlist, {}, 'GET').then(function (data) {
    //   if (data.type === 'success') {
    //     that.setData({
    // 	addressList: data.data.dataList
    //     });
    //   }
    // });
  },
  onReachBottom: function () {
    this.getAddressList();
  },
  addressSelect(event) {
    let checkedAddress = event.currentTarget.dataset.address;
    app.globalData.checkedAddress = checkedAddress;
    wx.navigateBack();
  }

})